package com.qf.ability.message.handler;

import com.alibaba.fastjson.JSON;
import com.qf.ability.message.entity.Message;
import com.qf.ability.message.handler.base.BaseInChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * 默认的入站消息处理器
 * author Ken
 * create_time 2022/10/16
 */
@Component
@Slf4j
@Order(0)
public class DefaultMsgInChannelHandler extends BaseInChannelHandler<TextWebSocketFrame> {
    @Override
    protected void hander(ChannelHandlerContext ctx, TextWebSocketFrame msg) {
        log.debug("[default handler] 接收到文本帧消息 - {}", msg.text());
        //进行消息转换
        Message message = JSON.parseObject(msg.text(), Message.class);
        //继续往后传递
        ctx.fireChannelRead(message);
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("有一个客户端连接了服务器....");
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("有一个客户端断开了连接....");
    }
}
